<?php
namespace jy_chengedu;
defined('BY_JYA') or exit('error');
/*
	本插件：$this->model('model')->方法名();
	其他地方调用：model('jy_chengedu/model')->方法名();
*/
class model extends \plugin{
	/*
		核实分销商
	*/
	public function checkschool($jigouid=0){
			$uid=$uid?:$_SESSION['uid'];
			$jigouid=$jigouid?:$_SESSION['jigouid'];
			if($jigouid){
				$school=pdo_get('jy_chengedu_jigou',array('id'=>$jigouid,'uniacid'=>$_SESSION['uniacid']));
				if(empty($school)){
					exi('没有找到该机构！','error','goback');
				}
			}else{
				$school=pdo_get('jy_chengedu_jigou',array('mid'=>$uid));
				if(empty($school)){
					exi('您还未注册机构','error','jy_chengedu/register/');
					/*$data =array(
						'applytime'=>time(),
						'logo'=>$member['avatar'],
						'mobile'=>$member['mobile'],
						'realname'=>$member['realname'],
						'title'=>$member['nickname'],
						'desc'=>'欢迎光临：我的小店《'.$member['nickname'].'小店》，我们将竭诚为您服务,祝您购物愉快！',
						'status'=>1,
						'uniacid'=>$_SESSION['uniacid'],
						'mid'=>$uid,
						'jointime'=>time(),
					);
					pdo_insert('jy_chengedu_jigou',$data);
					$school=pdo_get('jy_chengedu_jigou',array('mid'=>$uid));*/
				}elseif($school['status']!=1){
					$_SESSION['uid']='';
					$_SESSION['jigouid']='';
					exi('申请正在审核请耐心等待','error','jy_chengedu/register/');
				}else{
					$jigouid = $school['id'];
				}
			}
			return $school;
	}
	/*
		核实商品数量
	*/
	public function checkgoodsnum($jigouid=0,$isexi=false){
		$school=pdo_get('jy_chengedu_jigou',array('id'=>$jigouid,'uniacid'=>$_SESSION['uniacid']));
		if(!$school['groupid']){
			return true;
		}
		$group_goodsnum=pdo_getcolumn('jy_chengedu_group',array('id'=>$school['groupid']),'goodsnum');
		return true;
	}
	public function get($params,&$total=null,$lat_lng=array()){
		if(!$params['orderby']){
			$params['orderby']=array('id desc');
		}
		//$list=$this->pdo_getlist('user',$params,$total);
		$list=pdo_getall('jy_chengedu_jigou',$params['params'],$params['fields']);
		foreach($list as $k=>&$v){
			$v['logo']=tomedia($v['logo']);
			$v['lat_lng']=json_decode($v['lat_lng'],true);
			if($v['lat_lng']['lng'] && $v['lat_lng']['lat']){
				$v['juli']=cfc('tools')->getdistance($lat_lng['lng'],$lat_lng['lat'],$v['lat_lng']['lng'],$v['lat_lng']['lat']);
				$v['juli']=intval($v['juli']/1000);
			}else{
				if($lat_lng){
					unset($list[$k]);
				}
			}
		}
		paixu($list,'juli','asc');
		return $list;
	}
	public function getschool($parameter,$limit=5,$lat_lng=array(),$page=1,$limitjuli=0){
		$limitjuli=intval($limitjuli);
		if($lat_lng['lat'] || $lat_lng['lng']){
			$lat_lng=array('lat'=>$lat_lng['lat'],'lng'=>$lat_lng['lng']);
			$this->session('lat_lng',$lat_lng);
		}
		$lat_lng=$this->session('lat_lng');
		$total='';
		$parameter['orderby']=$parameter['orderby']?:'id desc';
		$list=pdo_getslice('jy_chengedu_jigou',$parameter['params'],$parameter['limit'],$total,$parameter['fields'],$parameter['key'],$parameter['orderby']);
		foreach($list as $k=>&$v){
			$v['pcd']=json_decode($v['pcd'],true);
			$v['lat_lng']=json_decode($v['lat_lng'],true);
			$v['lat_lng']['lng']=$v['lat_lng']['lng']?:'114.424632';
			$v['lat_lng']['lat']=$v['lat_lng']['lat']?:'23.126942';
			$v['juli']=cfc('tools')->getdistance($lat_lng['lng'],$lat_lng['lat'],$v['lat_lng']['lng'],$v['lat_lng']['lat']);
			$v['juli']=intval($v['juli']/1000);
			if($limitjuli && $v['juli']>$limitjuli){
				unset($list[$k]);
			}
		}
		paixu($list,'juli');
		if($parameter['orderbykey']){
			paixu($list,$parameter['orderbykey']);
		}
		if($limit){
			$list=array_slice($list,0,intval($limit));
		}else{
			$page=$page?:1;
			$start=($page-1)*10;
			$list=array_slice($list,$start,10);
		}
		return $list;
	}
	public function out_times($school_id,$post=array()) {
		$store=pdo_get('jy_chengedu_jigou',array('uniacid'=>$_SESSION['uniacid'],'id'=>$school_id));
		$store['onlinetime']=json_decode($store['onlinetime'],true);
		if($store['mypeisong']){
			$store['out_pay']=$store['out_pay']?:$post['out_pay'];
		}else{
			$store['out_pay']=$post['out_pay'];
		}
		$store['out_pay']=price($store['out_pay']);
		//配送时间
		$days = array(
				0=>date("m-d",time("day")),
				1=>date("m-d",strtotime("+1 day")),
				2=>date("m-d",strtotime("+2 day")), 
				3=>date("m-d",strtotime("+3 day")), 
				4=>date("m-d",strtotime("+4 day")),
				5=>date("m-d",strtotime("+5 day")),
				6=>date("m-d",strtotime("+6 day")),
		);
		$totaytime = strtotime(date('Y-m-d'));
		//配送时间段
		$lasttimebox=0;
		if($post['set_timebox']==1){
			foreach($post['peisong_timebox'] as &$item){
				$start=$daytime.' '.$item['times']['start'];
				$start=strtotime($start);
				$end=$daytime.' '.$item['times']['end'];
				$end=strtotime($end);
				$times[]=array(
					'start' => $item['times']['start'],
					'end' => $item['times']['end'],
					'timestamp' => $end,
					'fee' => 0,
					'timev' =>$item['times']['start'].'~'.$item['times']['end'],
					'out_pay' => $store['out_pay']."元配送费",
				);
				$timestamp[] = $end;
				if($end>$item['yuyue']*60+time()){
					$lasttimebox +=1;
				}
			}
		}else{
			$timestamp = array();
			$daytime=date('Y-m-d');
			if($store['onlinetime']['start']){
				$start=$daytime.' '.$store['onlinetime']['start'];
				$start=strtotime($start);
			}else{
				$start = mktime(8, 0);
			}
			if($store['onlinetime']['end']){
				$end=$daytime.' '.$store['onlinetime']['end'];
				$end=strtotime($end);
			}else{
				$end = mktime(22, 0);
			}
			for($i = $start; $i < $end;) {
					$times[] = array(
						'start' => date('H:i', $i),
						'end' => date('H:i', $i + 1800),
						'timestamp' => $i + 1800,
						'fee' => 0,
						'timev' =>date('H:i', $i).'~'.date('H:i', $i + 1800),
						'out_pay' => $store['out_pay']."元配送费",
					);
					$timestamp[] = $i + 1800;
					$i += 1800;
					if($i + 1800>30*60+time()){
						$lasttimebox +=1;
					}
			}
		}
		$data = array(
			'days' => $days,
			'times' => $times,
			'timestamp' => $timestamp,
			'updatetime' => strtotime(date('Y-m-d')) + 86400,
			'reserve' => 0,
			'lasttimebox' => $lasttimebox,
		);
		return $data;
	}
	public function current_level_up_types(){
		return str_array($this->zhaoshang['level_up_types']);
	}
	public function upcomments($postdata,$jigouid){
		$comment_id=$this->pdo_getcolumn('comments',array('jigouid'=>$jigouid,'orderid'=>intval($postdata['orderid']),'goodsid'=>intval($postdata['goodsid'])),'id');
		$data=array(
			'content'=>$postdata['content'],
			'images'=>ijson_encode($postdata['images']),
			'star'=>intval($postdata['star']),
			'sendstar'=>intval($postdata['sendstar']),
			'fuwustar'=>intval($postdata['fuwustar']),
		);
		if($comment_id){
			$this->pdo_update('comments',$data,array('id'=>$comment_id));
		}else{
			$show=$this->setting['iscomment']?0:1;
			$insert=array(
				'uniacid'=>$_SESSION['uniacid'],
				'mid'=>$_SESSION['uid'],
				'show'=>$show,
				'jigouid'=>$jigouid,
				'goodsid'=>intval($postdata['goodsid']),
				'orderid'=>intval($postdata['orderid']),
				'createtime'=>TIMESTAMP,
			);
			$data=array_merge($insert,$data);
			$this->pdo_insert('comments',$data);
			$comment_id=pdo_insertid();
		}
		return $comment_id;
	}
}